package com.myjava.lift;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Set;

/**
 * @auther: tao
 * @date: 2018/10/16 16:54
 * @description: 电梯运行实现类
 */
public class LiftServiceImpl implements LiftService {
    @Autowired
    private CacheInstance cacheInstance;

    @Override
    public void liftRun() {
        //首先确定电梯所在楼层
        int liftFloor = 1;
        if (null != cacheInstance.get("liftFloor")) {
            liftFloor = (Integer) cacheInstance.get("liftFloor");
        }

        //确定电梯运转状态（0：静止， 1：上升， 2：下降）

        int liftstate = 0;
        if (null != cacheInstance.get("liftstate")) {
            liftstate = (Integer) cacheInstance.get("liftstate");
        }

        //电梯运行
        //1、首先得到坐电梯的人要去的楼层队列
        Set<Integer> goSet = new HashSet<>();
        if (null != cacheInstance.get("goSet")) {
            goSet = (Set<Integer>) cacheInstance.get("goSet");
        }

        //2、确定电梯将要上升或下降
        do {
            liftstate = (Integer) cacheInstance.get("liftstate");
            if (goSet.size() > 0) {
                switch (liftstate) {
                    case 0:
                        for (Integer go : goSet) {
                            cacheInstance.put("liftstate", 1);
                            System.out.println("电梯所在层数：" + liftFloor);
                            return;
                        }
                        break;
                    case 1:
                        System.out.println("1");
                        break;
                    case 2:
                        System.out.println("2");
                        break;
                    default:
                        System.out.println("default");
                        break;
                }

            }
        } while (liftstate != 0);

        //电梯停止
    }
}
